package com.brozha.ans_sys.interceptor;

import com.brozha.ans_sys.exception.AnsSysException;
import com.brozha.ans_sys.utils.JWTUtil;
import com.brozha.ans_sys.utils.Result;
import com.brozha.ans_sys.utils.ResultCode;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author brozha
 * @date 2021/4/4
 */
public class JWTInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("token");
        try {
            JWTUtil.verify(token);
            return true;
        }catch (Exception e){
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            throw new AnsSysException(ResultCode.TOKEN_ERROR,"token错误");
            //return false;
        }

    }
}